﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TestMySqlConnetPoll;
using DevExpress.XtraEditors.Repository;
using System.Threading;
using View.Common;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors.Controls;

namespace View.Chart.WDW
{
    public partial class ExternalNetworkFaultCurve : DevExpress.XtraEditors.XtraForm
    {
        private WdwSjddTable wdw = new WdwSjddTable();
        private float now_dotnum;
        public ExternalNetworkFaultCurve()
        {
            InitializeComponent();

            //初始化曲线名称
            IniCurveNameList();
        }

        /// <summary>
        /// 查找数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CBoxCurveTime_SelectedIndexChanged(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            
        }

        /// <summary>
        /// 添加一条曲线
        /// </summary>
        /// <param name="seriesName"></param>
        /// <param name="chart"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        private void CreateSeries(string seriesName, ChartControl chart, List<ObjValue> values ,bool showInLegend)
        {
            //初始化Series
            Series series = new Series();
            //series.ArgumentScaleType = ScaleType.DateTime;
            series.ValueScaleType = ScaleType.Numerical;
            series.Label.Border.Visible = false;
            //series.Label.BackColor = Color.Yellow;
            //series.View.Color = Color.Yellow;
            series.CrosshairLabelVisibility = DevExpress.Utils.DefaultBoolean.False;
            series.ChangeView(ViewType.Line);
            series.CrosshairEnabled = DevExpress.Utils.DefaultBoolean.False;
            series.CrosshairHighlightPoints = DevExpress.Utils.DefaultBoolean.False;
            series.LegendText = seriesName;
            series.ShowInLegend = showInLegend;
            series.ToolTipEnabled = DevExpress.Utils.DefaultBoolean.False;

            chart.Series.Add(series);

            List<SeriesPoint> points = new List<SeriesPoint>();
            int index = 0;

            foreach (ObjValue qxA in values)
            {
                index++;
                SeriesPoint point = new SeriesPoint(index, qxA.Value);
                points.Add(point);
            }

            if (points.Count > 0)
                AddPoints(series, points);
        }

        /// <summary>
        /// 给一条曲线添加一组点
        /// </summary>
        /// <param name="series"></param>
        /// <param name="pointsToUpdate"></param>
        private void AddPoints(Series series, List<SeriesPoint> pointsToUpdate)
        {
            series.Points.AddRange(pointsToUpdate.ToArray());
        }

        /// <summary>
        /// 初始化曲线名称列表
        /// </summary>
        private void IniCurveNameList()
        {
            foreach(string strkey in c_Device.WdwErrorQX_DevList)
                this.repositoryItemComboBox2.Items.Add(strkey);

            if (repositoryItemComboBox2.Items.Count > 0)
                this.cBoxCurveName.EditValue = repositoryItemComboBox2.Items[0];
        }

        private void BtnPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void BtnExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void BtnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        /// <summary>
        /// 自定义坐标表示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Chart_Dl_CustomDrawAxisLabel(object sender, CustomDrawAxisLabelEventArgs e)
        {
            AxisBase axis = e.Item.Axis;
            if (axis is AxisY || axis is AxisY3D || axis is RadarAxisY)
            {
                //double axisValue = (double)e.Item.AxisValue;
                e.Item.Text = e.Item.Text + "A";
            }
        }

        private void Chart_dy_CustomDrawAxisLabel(object sender, CustomDrawAxisLabelEventArgs e)
        {
            AxisBase axis = e.Item.Axis;
            if (axis is AxisY || axis is AxisY3D || axis is RadarAxisY)
            {
                //double axisValue = (double)e.Item.AxisValue;
                e.Item.Text = e.Item.Text + "V";
            }
        }

        /// <summary>
        /// 曲线设备类型改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CBoxCurveName_EditValueChanged(object sender, EventArgs e)
        {
            LoadTimeList();

            //CreateCurveData();
        }

        private void LoadTimeList()
        {
            repositoryItemComboBox3.Items.Clear();
            this.cBoxCurveTime.EditValue = "";

            wdw.ReadTimeList((string)this.cBoxCurveName.EditValue);

            while (!wdw.ReadyTimeList)
            {
                if (wdw.ErrorFlag != 0)
                {
                    MessageBox.Show("查询不到对应时间列表");
                    return;
                }

                Thread.Sleep(20);
            }
            

            foreach (DateTime dt in wdw.TimeList)
            {
                ComboBoxItem item = new ComboBoxItem();
                repositoryItemComboBox3.Items.Add(UtilityFunc.DateTimeStringFormat(dt, TimeFormatType.DCGONGLV));
            }

            if (repositoryItemComboBox3.Items.Count > 0)
                this.cBoxCurveTime.EditValue = repositoryItemComboBox3.Items[0];
        }

        private void CreateCurveData()
        {

            chart_dl.Series.Clear();
            chart_dy.Series.Clear();
            if (((string)this.cBoxCurveTime.EditValue) == "")
            {
                CreateSeries("", chart_dl, new List<ObjValue>(), false);
                CreateSeries("", chart_dy, new List<ObjValue>(), false);
                return;
            }

            wdw.ReadValueList((string)this.cBoxCurveName.EditValue, DateTime.Parse((string)this.cBoxCurveTime.EditValue));
            while (!wdw.ReadyValueList)
            {
                if (wdw.ErrorFlag != 0)
                    return;

                Thread.Sleep(20);
            }

            ObjWdwSjddqx objwdw = wdw.ValueList[DateTime.Parse((string)this.cBoxCurveTime.EditValue)];
            //now_dotnum = objwdw.dotnum;

            CreateSeries((string)this.cBoxCurveName.EditValue + "A相电流", chart_dl, objwdw.dla,true);
            CreateSeries((string)this.cBoxCurveName.EditValue + "B相电流", chart_dl, objwdw.dlb,true);
            CreateSeries((string)this.cBoxCurveName.EditValue + "C相电流", chart_dl, objwdw.dlc,true);

            CreateSeries((string)this.cBoxCurveName.EditValue + "A相电压", chart_dy, objwdw.dya, true);
            CreateSeries((string)this.cBoxCurveName.EditValue + "B相电压", chart_dy, objwdw.dyb, true);
            CreateSeries((string)this.cBoxCurveName.EditValue + "C相电压", chart_dy, objwdw.dyc, true);

            //初始化X坐标
            (chart_dl.Diagram as XYDiagram).AxisX.WholeRange.Auto = false;
            (chart_dl.Diagram as XYDiagram).AxisX.WholeRange.AutoSideMargins = false;
            (chart_dl.Diagram as XYDiagram).AxisX.WholeRange.SetMinMaxValues(0, 50);
            (chart_dl.Diagram as XYDiagram).AxisX.WholeRange.SideMarginsValue = 0;
        }

        /// <summary>
        /// 曲线时间改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CBoxCurveTime_EditValueChanged(object sender, EventArgs e)
        {
            CreateCurveData();
        }
    }
}